
package com.learn.hibernatevalidator.chapter12.failfastonpropertyviolation;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Constraint(validatedBy = {NonSelfPublishing.NonSelfPublishingValidator.class})
@Target({TYPE})
@Retention(RUNTIME)
public @interface NonSelfPublishing {

    String message() default "{org.hibernate.validator.referenceguide.chapter12.failfastonpropertyviolation.NonSelfPublishing.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class NonSelfPublishingValidator implements ConstraintValidator<NonSelfPublishing, Book> {

        @Override
        public boolean isValid(Book book, ConstraintValidatorContext context) {
            return !book.getAuthor().equals(book.getPublisher());
        }
    }
}
